package edu.dayu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.dayu.mapper.DictMapper;
import edu.dayu.model.cmn.Dict;
import edu.dayu.service.DictDubbo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;


@DubboService
public class DictDubboImpl extends ServiceImpl<DictMapper,Dict> implements DictDubbo {
    @Autowired
    private DictMapper dictMapper;

    @Override
    public String getNameByParentDictCodeAndValue(String parentDictCode, String value) {
        //如果value能唯一定位数据字典，parentDictCode可以传空，例如：省市区的value值能够唯一确定
        if(StringUtils.isEmpty(parentDictCode)) {
            Dict dict = baseMapper.selectOne(new QueryWrapper<Dict>().eq("value",value));
            if(null != dict) {
                return dict.getName();
            }
        } else {
            Dict parentDict = baseMapper.selectOne(new QueryWrapper<Dict>().eq("dict_code",parentDictCode));
            if(null == parentDict)
                return "";
            Dict dict = baseMapper.selectOne(new QueryWrapper<Dict>().eq("parent_id",parentDict.getId()).eq("value",value));
            if(null != dict) {
                return dict.getName();
            }
        }
        return "";
    }

}
